package client

import (
	"fmt"
	"gitee.com/enjoy_5/sigin-rpc/constant"
	pb "gitee.com/enjoy_5/sigin-rpc/proto/clockin" //注意这个路径
	"gitee.com/xuxinzhi2206/corehub/nacos"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
)

// 1.连接服务端
// 2.实例gRPC客户端
// 3.调用

func client(hand func(c pb.ClockInClient) error) error {
	service, err := nacos.FindHealthService(constant.SERVICE, constant.GroupName)
	if err != nil {
		return fmt.Errorf("find server err:%v", err)
	}
	fmt.Println("发现服务：：：", service)
	conn, err := grpc.Dial(service, grpc.WithInsecure())
	if err != nil {
		return fmt.Errorf("connect server 连接异常:%v", err)
	}
	defer conn.Close()
	s := pb.NewClockInClient(conn)
	return hand(s)
}
func CreateClockIn(in *pb.ClockInReq) (resp *pb.ClockInResp, err error) {
	return resp, client(func(client pb.ClockInClient) error {
		resp, err = client.ClockInCreate(context.Background(), in)
		if err != nil {
			return err
		}
		return nil
	})
}
func CreateRetroactiveClocking(in *pb.RetroactiveClockInReq) (resp *pb.RetroactiveClockInResp, err error) {
	return resp, client(func(client pb.ClockInClient) error {
		resp, err = client.RetroactiveClockIn(context.Background(), in)
		if err != nil {
			return err
		}
		return nil
	})
}
func GetClockIn(in *pb.GetClockInReq) (resp *pb.GetClockInResp, err error) {
	return resp, client(func(client pb.ClockInClient) error {
		resp, err = client.GetClockIn(context.Background(), in)
		if err != nil {
			return err
		}
		return nil
	})
}
func GetClockInfo(in *pb.GetClockInfoReq) (resp *pb.GetClockInfoResp, err error) {
	return resp, client(func(client pb.ClockInClient) error {
		resp, err = client.GetClockInfo(context.Background(), in)
		if err != nil {
			return err
		}
		return nil
	})
}
